home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicProgressBarUI.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  169 lines

  1. /*
  2.  * @(#)OrganicProgressBarUI.java    1.8 98/02/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.plaf.basic.*;
  24. import com.sun.java.swing.*;
  25. import com.sun.java.swing.event.*;
  26. import java.awt.*;
  27. import com.sun.java.swing.plaf.*;
  28.  
  29.  
  30. /**
  31.  * A Java L&F implementation of ProgressBarUI.  This implementation 
  32.  * is both the view and the controller.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.8 02/05/98
  42.  * @author Michael C. Albers
  43.  */
  44. public class OrganicProgressBarUI extends BasicProgressBarUI {
  45.     int pending;
  46.     static protected final Dimension PREFERRED_INNER_HORIZONTAL 
  47.                                      = new Dimension(144, 11);
  48.     static protected final Dimension PREFERRED_INNER_VERTICAL
  49.                                      = new Dimension(11, 144);
  50.     int cellLength, cellSpacing;
  51.     protected Color headColor;
  52.  
  53.  
  54.     public static ComponentUI createUI(JComponent c) {
  55.     return new OrganicProgressBarUI();
  56.     }
  57.   
  58.     public void installUI(JComponent c) {
  59.     super.installUI(c);
  60.     headColor = UIManager.getColor("ProgressBar.head");
  61.     }
  62.     
  63.     public int getCellLength() {
  64.     return 2;
  65.     }
  66.     
  67.     public int getCellSpacing() {
  68.     return 1;
  69.     }
  70.     
  71.     public void paint(Graphics g, JComponent c) {
  72.     Dimension totalSize = c.getSize();
  73.     Dimension innerSize;
  74.     int x, y;
  75.     int span, length, max, min, current, increment;
  76.     JProgressBar progressBar = (JProgressBar)c;
  77.     BoundedRangeModel model = progressBar.getModel();
  78.     Insets b = getBorderInsets(c);
  79.     cellLength = getCellLength();
  80.     cellSpacing = getCellSpacing();
  81.     
  82.     g.setColor(c.getForeground());
  83.     if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  84.         x = b.left;
  85.         y = b.top;
  86.         innerSize = new Dimension(totalSize.width - (b.left + b.right),
  87.                       totalSize.height - (b.top + b.bottom));
  88.         span = model.getMaximum() - model.getMinimum();
  89.         increment = cellLength + cellSpacing;
  90.     
  91.         length = 0;
  92.         if (span != 0) {
  93.             double bigWidth = innerSize.width;
  94.             double bigValue = model.getValue();
  95.             double fractionComplete = bigValue / span;
  96.             length = (int)(bigWidth * fractionComplete);
  97.         }
  98.  
  99.         max = (x + length) - cellLength;
  100.         
  101.         // Draw constant left cell
  102.         g.fillRect(x, y, cellLength, innerSize.height);
  103.         // Draw constant right cell
  104.         g.fillRect(totalSize.width-cellLength, y, 
  105.                cellLength, innerSize.height);
  106.         
  107.         // Draw the cells
  108.         for (current = x; current < max; current += increment) {
  109.         g.fillRect(current, y, cellLength, innerSize.height);
  110.         }
  111.         
  112.         if (model.getValue() != 0) {
  113.         // Draw the head cell
  114.         g.setColor(headColor);
  115.         // Ensure that "head" is always drawn at the end
  116.         if (model.getValue() == model.getMaximum()) {
  117.             current = innerSize.width - cellLength;
  118.         }
  119.         g.fillRect(current, y, cellLength, innerSize.height);
  120.         }
  121.     } else {     // Must be VERTICAL
  122.         x = b.left;
  123.         y = b.top;
  124.         innerSize = new Dimension(totalSize.width - (b.left + b.right),
  125.                       totalSize.height - (b.top + b.bottom));
  126.         span = model.getMaximum() - model.getMinimum();
  127.         increment = cellLength + cellSpacing;
  128.     
  129.         length = 0;
  130.         if (span != 0) {
  131.             double bigHeight = innerSize.height;
  132.             double bigValue = model.getValue();
  133.             double fractionComplete = bigValue / span;
  134.             length = (int)(bigHeight * fractionComplete);
  135.         }
  136.  
  137.         min = ((innerSize.height - 1) + y) - length;
  138.         
  139.         // Draw the constant top cell
  140.         g.fillRect(x, y, innerSize.width, cellLength);
  141.         // Draw the constant bottom cell
  142.         g.fillRect(x, totalSize.height-cellLength,
  143.                innerSize.width, cellLength);
  144.         
  145.         for (current = (innerSize.height - 1) + (y - cellLength);
  146.          current > min; current -= increment)
  147.         {
  148.         g.fillRect(x, current, innerSize.width, cellLength);
  149.         }
  150.         
  151.         if (model.getValue() != 0) {
  152.         // Draw the head cell
  153.         g.setColor(headColor);
  154.         // Ensure that "head" is always drawn at the end
  155.         if (model.getValue() == model.getMaximum()) {
  156.             current = y;
  157.         }
  158.         g.fillRect(x, current, innerSize.width, cellLength);
  159.         }
  160.     }
  161.     }
  162.     
  163.     public Insets getBorderInsets(JComponent c) {
  164.     // no borders on OrganicProgressBar
  165.     return new Insets(0, 0, 0, 0);
  166.     }
  167.     
  168. }
  169.